package com.tanhua.sso.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.sso.service.FaceEngineService;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.service.UserService;
import com.tanhua.common.vo.PicUploadResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private PicUploadService picUploadService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private FaceEngineService faceEngineService;

    @Override
    public boolean saveUserInfo(Map<String, String> map, String token) {
        //校验token
        User user = this.userService.queryUserByToken(token);

        if (null == user) {
            return false;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setSex(StringUtils.equalsIgnoreCase(map.get("gender"), "man") ? SexEnum.MAN : SexEnum.WOMAN);
        userInfo.setNickName(map.get("nickname"));
        userInfo.setBirthday(map.get("birthday"));
        userInfo.setCity(map.get("city"));

        return this.userInfoMapper.insert(userInfo) == 1;
    }

    @Override
    public Boolean saveUserLogo(String token, MultipartFile headPhoto) {
        //校验token
        User user = userService.queryUserByToken(token);
        if (user == null) {
            return false;
        }

        //人脸验证
        try {
            boolean b = faceEngineService.checkIsPortrait(headPhoto.getBytes());
            if (!b) {
                return false;
            }
        } catch (IOException e) {
            log.error("人脸识别失败 + token = " + token + e);
            return false;
        }

        //上传图片到阿里云，并返回图片保持的地址
        PicUploadResult picUploadResult = this.picUploadService.upload(headPhoto);
        if (StringUtils.isEmpty(picUploadResult.getName())) {
            return false;
        }

        //把图片地址保存到userInfo中
        UserInfo userInfo = new UserInfo();
        userInfo.setLogo(picUploadResult.getName());
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId());

        //测试用例上传头像时,同一个id的用户有了2个userinfo,测试时会一次性修改2条记录,所以使用==1不行
//        return userInfoMapper.update(userInfo, wrapper) == 1;
        return userInfoMapper.update(userInfo, wrapper) != 0;
    }


}
